/*
 * FloorQuad.java
 *
 * Created on 18. Februar 2007, 15:30
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.graphics;

import java.nio.FloatBuffer;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.TriMesh;
import com.jme.scene.batch.TriangleBatch;
import com.jme.util.geom.BufferUtils;

/**
 * <code>Quad</code> defines a four sided, two dimensional shape. The local
 * height of the <code>Quad</code> defines it's size about the y-axis, while
 * the width defines the x-axis. The z-axis will always be 0.
 * 
 * <code>FloorQuad</code> is the same as <code>Quad</code>, but in the x-z plane,
 * and with the possibility to flip normals. This class is needed because
 * GeometryBatch currently doesn't support rotation of <code>TriMesh</code>s.
 *
 * @author Mark Powell
 * @author Daniel Gronau
 */
public class FloorQuad extends TriMesh {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * Constant for normals pointing up (e.g. for a floor)
     */
    public static final int UP = 1;
    /**
     * Constant for normals pointing down (e.g. for a ceiling)
     */
    public static final int DOWN = -1;
    
    /**
     * For internal use only
     */
    public FloorQuad() {
        
    }
    
    /**
     * Constructor creates a new <code>FloorQuad</code> object. That data for the
     * <code>FloorQuad</code> is not set until a call to <code>initialize</code>
     * is made.
     * @param name the name of this <code>FloorQuad</code>.
     */
    public FloorQuad(String name) {
        super(name);
    }
    
    /**
     * Constructor creates a new <code>Quade</code> object with the provided
     * width and height.
     *
     * @param name the name of the <code>Quad</code>.
     * @param width the width of the <code>Quad</code>.
     * @param height the height of the <code>Quad</code>.
     * @param norms the direction of the normals
     */
    public FloorQuad(String name, float width, float height, int norms) {
        super(name);
        initialize(width, height, norms);
    }
    
    /**
     * <code>resize</code> changes the width and height of the given quad by
     * altering its vertices.
     *
     * @param width the new width of the <code>Quad</code>.
     * @param height the new height of the <code>Quad</code>.
     */
    public void resize(float width, float height) {
        TriangleBatch batch = getBatch(0);
        batch.getVertexBuffer().clear();
        batch.getVertexBuffer().put(width / 2f).put(0).put(height / 2f);
        batch.getVertexBuffer().put(width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(height / 2f);
    }
    
    /**
     * 
     * <code>initialize</code> builds the data for the <code>FloorQuad</code>
     * object.
     * @param width the width of the <code>Quad</code>.
     * @param height the height of the <code>Quad</code>.
     * @param norms the direction of the normals
     * @see FloorQuad.UP
     * @see FloorQuad.DOWN
     */
    public void initialize(float width, float height, int norms) {
        TriangleBatch batch = getBatch(0);
        batch.setVertexCount(4);
        batch.setVertexBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
        batch.setNormalBuffer(BufferUtils.createVector3Buffer(batch.getVertexCount()));
        FloatBuffer tbuf = BufferUtils.createVector2Buffer(batch.getVertexCount());
        setTextureBuffer(0,tbuf);
        batch.setTriangleQuantity(2);
        batch.setIndexBuffer(BufferUtils.createIntBuffer(batch.getTriangleCount() * 3));
        
        batch.getVertexBuffer().put(width / 2f).put(0).put(height / 2f);
        batch.getVertexBuffer().put(width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(-height / 2f);
        batch.getVertexBuffer().put(-width / 2f).put(0).put(height / 2f);
        
        batch.getNormalBuffer().put(0).put(norms).put(0);
        batch.getNormalBuffer().put(0).put(norms).put(0);
        batch.getNormalBuffer().put(0).put(norms).put(0);
        batch.getNormalBuffer().put(0).put(norms).put(0);
        
        
        tbuf.put(0).put(1);
        tbuf.put(0).put(0);
        tbuf.put(1).put(0);
        tbuf.put(1).put(1);
        
        setDefaultColor(ColorRGBA.white);
        
        batch.getIndexBuffer().put(0);
        batch.getIndexBuffer().put(1);
        batch.getIndexBuffer().put(2);
        batch.getIndexBuffer().put(0);
        batch.getIndexBuffer().put(2);
        batch.getIndexBuffer().put(3);
    }
    
    /**
     * <code>getCenter</code> returns the center of the <code>FloorQuad</code>.
     *
     * @return Vector3f the center of the <code>FloorQuad</code>.
     */
    public Vector3f getCenter() {
        return worldTranslation;
    }
}